package ten;

public class ThreadLocalTest02 {

    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 2);
    private static ThreadLocal<Integer> threadLocal1 = new InheritableThreadLocal<>();

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new MyRun()).start();
        }
        threadLocal1.set(10);
        System.out.println(Thread.currentThread().getName() + "--->" + threadLocal1.get());
        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "--->" + threadLocal1.get());
            threadLocal1.set(99);
            System.out.println(Thread.currentThread().getName() + "--->" + threadLocal1.get());
        }).start();
    }

    public static class MyRun implements Runnable {
        public MyRun() {
            threadLocal.set(100);
            System.out.println(Thread.currentThread().getName() + "--->" + threadLocal.get());
        }

        @Override
        public void run() {
            int left = threadLocal.get();
            System.out.println(Thread.currentThread().getName() + "--->" + left);
            threadLocal.set(left - 1);
            System.out.println(Thread.currentThread().getName() + "--->" + threadLocal.get());
        }
    }
}
